package com.guigu.controller;

import com.guigu.entity.CheckitemBean;
import com.guigu.entity.OrderBean;
import com.guigu.entity.PackageBean;
import com.guigu.service.CheckitemService;
import com.guigu.service.PackageService;
import com.guigu.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/package")
public class PackageController {

    @Autowired
    private PackageService packageService;

    @Autowired
    private CheckitemService checkitemService;

    private static final String filePath=System.getProperty("user.dir")+"/file/";
    @GetMapping("/index")
    public String index() {
        return "index";
    }

    @PostMapping("/Queryitemsbasedonid")
    public Result<List<CheckitemBean>> Queryitemsbasedonid(@RequestBody PackageBean packageBean) {



        List<CheckitemBean> CheckitemBeanList = this.checkitemService.queryitemsbasedonid(packageBean);


        return new Result<>("200", "true", "成功查询用户列表", CheckitemBeanList);
    }


    @GetMapping("/findAll")
    public Result<List<PackageBean>> findAll() {

        List<PackageBean> PackageBeanList = this.packageService.selectPackage();


        return new Result<>("200", "true", "成功查询用户列表", PackageBeanList);
    }

    @GetMapping("/findCheckitemVOAll")
    public Result<List<CheckitemVO>> findCheckitemVOAll() {

        List<CheckitemVO> CheckitemVOList = this.packageService.selectCheckitemVO();


        return new Result<>("200", "true", "成功查询用户列表", CheckitemVOList);
    }

    @GetMapping("/findCheckgroupVOAll")
    public Result<List<CheckgroupVO>> findCheckgroupVOAll() {

        List<CheckgroupVO> CheckgroupVOList = this.packageService.selectCheckgroupVO();


        return new Result<>("200", "true", "成功查询用户列表", CheckgroupVOList);
    }

    @PostMapping("/uploadForm")
    public Result uploadForm(@RequestParam("file") MultipartFile file) {
        System.out.println("文件："+file);

        // 获取当前日期
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String currentDate = dateFormat.format(new Date());

        // 获取上传文件的原始文件名，并拼接当前日期
        String originalFilename = file.getOriginalFilename();
        String fileName = currentDate + "_" + originalFilename;

        FileOutputStream fos = null;
        InputStream fis = null;
        try {
            // 构造文件存储路径
            String filePath = "C:\\Users\\30956\\Desktop\\Gitee-Ssm-Project\\web\\src\\main\\resources\\Packageimage\\" + fileName;

            System.out.println("文件："+file);

            fos = new FileOutputStream(filePath);
            fis = file.getInputStream();
            byte[] bytes = new byte[1024];
            int read;
            while ((read = fis.read(bytes)) != -1) {
                fos.write(bytes, 0, read);
            }
        } catch (Exception e) {
            System.out.println(e);
            return new Result<>("500", "true", "异常", "异常");
        } finally {
            try {
                if (fos != null) {
                    fos.close();
                }
                if (fis != null) {
                    fis.close();
                }
            } catch (Exception e) {
                System.out.println(e);
                return new Result<>("501", "true", "异常", "异常");
            }
        }

        return new Result<>("200", "true", "文件上传成功", "文件上传成功");
    }




    @PostMapping("/addPackage")
    public Result addPackage(@ModelAttribute tjPackageVO formData) {


        MultipartFile file = formData.getFile();

        List<CheckitemVO> checkitemVOList = formData.getCheckitemVOList();
        List<CheckgroupVO> checkgroupVOList = formData.getCheckgroupVOList();

        Set<Integer> distinctProjectGroups = new HashSet<>();
        for (CheckitemVO checkitemVO : checkitemVOList) {
            Integer projectGroup = checkitemVO.getCheckgroupid();
            distinctProjectGroups.add(projectGroup);
        }

        System.out.println("distinctProjectGroups项目结果集：" + distinctProjectGroups.size());
        System.out.println("checkitemVOList项目结果集：" + checkitemVOList);
        System.out.println("checkgroupVOList项目组cd：" + checkgroupVOList.size());

        System.out.println("文件：" + file);



        // 获取当前日期
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String currentDate = dateFormat.format(new Date());
        // 获取上传文件的原始文件名，并拼接当前日期
        String originalFilename = file.getOriginalFilename();
        String fileName = currentDate + "_" + originalFilename;
        FileOutputStream fos = null;
        InputStream fis = null;
        try {
            // 构造文件存储路径
            String filePath = "C:\\Users\\30956\\Desktop\\Gitee-Ssm-Project\\web\\src\\main\\resources\\Packageimage\\" + fileName;

            fos = new FileOutputStream(filePath);
            fis = file.getInputStream();
            byte[] bytes = new byte[1024];
            int read;
            while ((read = fis.read(bytes)) != -1) {
                fos.write(bytes, 0, read);
            }
        } catch (Exception e) {
            System.out.println(e);
            return new Result<>("500", "true", "异常", "异常");
        } finally {
            try {
                if (fos != null) {
                    fos.close();
                }
                if (fis != null) {
                    fis.close();
                }
            } catch (Exception e) {
                System.out.println(e);
                return new Result<>("501", "true", "异常", "异常");
            }
        }


        PackageBean packageBean = new PackageBean(
                0,
                formData.getPackagename(),
                formData.getPackagecoding(),
                formData.getMnemonicname(),
                formData.getApplytogender(),
                formData.getAgexx()+"-"+formData.getAgesx(),
                Float.parseFloat(formData.getPackageprice()),
                formData.getIntroduce(),
                formData.getNote(),
                fileName,null);

        this.packageService.addPackage(packageBean);
        System.out.println("自增列：" + packageBean.getId());

        for (Integer xmzid : distinctProjectGroups) {
            this.packageService.addcheckgroup(packageBean.getId(),xmzid);
            for (CheckitemVO xmid : checkitemVOList) {
                this.packageService.addcheckitem(xmzid,xmid.getCheckitemid());
            }
        }

        for (CheckgroupVO xmzid : checkgroupVOList) {
            this.packageService.addcheckgroup(packageBean.getId(),xmzid.getCheckgroupid());
            List<CheckitemVO> cxdxm=this.packageService.selectCheckitemVOByid(xmzid.getCheckgroupid());
            for (CheckitemVO xmid : cxdxm) {
                this.packageService.addcheckitem(xmzid.getCheckgroupid(),xmid.getCheckitemid());
            }
        }


        return new Result<>("200", "true", "添加成功", "添加成功");
    }

    @PostMapping("/sctc")
    public Result sctc(@RequestBody PackageBean packageBean ) {
        // 处理 POST 请求的逻辑代码

        System.out.println("对象：" + packageBean);

        this.packageService.deletesetmealByid(packageBean);

        return new Result<>("200", "true", "操作成功", "操作成功");
    }

//    @PostMapping("/upLoad")
//    public Result upload(MultipartFile file) {
//        synchronized (FileController.class){
//            //获取当前时间戳
//            String flag = System.currentTimeMi`llis()+"";
//            // 获取原始文件名(就是你上传的文件本身的名字)
//            String fileName = file.getOriginalFilename();
//            try {
//                // 如果没有file文件夹，会给你在项目根目录下创建一个file文件夹
//                if(!FileUtil.isDirectory(filePath)){
//                    FileUtil.mkdir(filePath);
//
//                }
//                // 文件存储形式：时间戳-文件名
//                FileUtil.writeBytes(file.getBytes(), filePath + flag +"-"+ fileName);
//                System.out.println(fileName +"--上传成功");
//                Thread.sleep( 1L);
//            }
//            catch (Exception e) {
//                System.err.println(fileName +"--文件上传失败");
//
//            }
//
//            return Result.success(flag);
//        }
//
//    }

}
